package com.guchenghao.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guchenghao.dto.UserDto;
import com.guchenghao.entity.User;
import com.guchenghao.mapper.UserMapper;
import com.guchenghao.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @User 20675
 * @Author gch
 * @Date 2025/4/1 14:45
 * @Description <br />
 * <p>
 * <br />
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public Boolean login(User user, HttpSession session) {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("username", user.getUsername());
        wrapper.eq("password", user.getPassword());
        User userDb = userMapper.selectOne(wrapper);
        if (userDb == null) {
            return false;
        }
        UserDto userDto = BeanUtil.copyProperties(userDb, UserDto.class);
        session.setAttribute("userDto", userDto);
        return true;
    }

    @Override
    public UserDto checkLogin(HttpSession session) {
        Object userDto = session.getAttribute("userDto");
        if (userDto == null) {
            return null;
        }
        return (UserDto) userDto;
    }

    @Override
    public Boolean register(User user) {
        System.out.println(user);
        if (StrUtil.isBlank(user.getUsername()))
            user.setUsername("momo");
        if (StrUtil.isBlank(user.getPassword()))
            user.setPassword("666");
        return userMapper.insert(user) > 0;
    }
}
